﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Neuron.Cell;
using Neuron.Cell.Control;
using System.Xml;

namespace Neuron.NetworkDesigner
{
    public partial class FormMain : Form
    {
        private ctrlPerceptron _connectStart = null;
        private ctrlPerceptron _connectEnd = null;

        private PerceptronBackbone _backbone = null;

        public FormMain()
        {
            InitializeComponent();

            //ctrlTeachingConsole1.DrawingBoard = panelScreen;
            this.ctrlTeachingConsole1.DrawingBoard = ctrlPerceptronDrawingBoard1.MainScreen;
            this.ctrlBaseNetwork1.DrawingBoard = ctrlPerceptronDrawingBoard1;
            this.ctrlTeachingConsole1.UpdateGrid();
            this.ctrlPerceptronDrawingBoard1.UpdateNumberOfPerceptrons += this.UpdateNumberOfPerceptrons;
            this.toolStripStatusLabel3.Text = "Perceptrons: 0";
            this.toolStripStatusLabel4.Text = "Events: 0";
            this.ctrlTeachingConsole1.StepDone += this.ctrlStatistic1.calculateCriteriaDelta;
            this.ctrlPerceptronDrawingBoard1.UpdateNumberOfPerceptrons += ctrlStatistic1.updatePerceptronComboBox;
            this.ctrlStatistic1.DrawingBoard = ctrlPerceptronDrawingBoard1;

            this._backbone = new PerceptronBackbone();
            this.ctrlPerceptronDrawingBoard1.setBackbone(this._backbone);
            this.ctrlBaseNetwork1.setBackbone(this._backbone);
            this.ctrlTeachingConsole1.setBackbone(this._backbone);

            this.ctrlTeachingConsole1.StepDone += this.StepDone;

            this.ctrlPerceptronDrawingBoard1.FilenameUpdated += this.PrintNetworkFilename;
            this.ctrlTeachingConsole1.FilenameUpdated += this.PrintScriptFilename;

            toolStripStatusLabel1.Text = "Network: none";
            toolStripStatusLabel2.Text = "Script: none";



        }

        private void PrintNetworkFilename(string filename)
        {
            toolStripStatusLabel1.Text = "Network: " + filename;
        }

        private void PrintScriptFilename(string filename)
        {
            toolStripStatusLabel2.Text = "Script: " + filename;
        }

        private void UpdateNumberOfPerceptrons(int number)
        {
            toolStripStatusLabel3.Text = "Perceptrons: " + Convert.ToString(number);
            ctrlTeachingConsole1.UpdateGrid();
        }
        
        private void pictureBoxHLIM_ON_MouseDown(object sender, MouseEventArgs e)
        {
            String strData = (String)((Control)sender).Tag;
            pictureBoxHLIM_ON.DoDragDrop(strData, DragDropEffects.Copy | DragDropEffects.Move);
        }

        private void PerceptronControlClicked(ctrlPerceptron p)
        {
            if (this._connectStart != null)
            {
                this._connectEnd = p;
            } else
            {
                this._connectStart = p;
            }

            if ((this._connectStart != null)&&(this._connectEnd != null))
            {
                Perceptron perceptronStart = this._connectStart.Perceptron;
                Perceptron perceptronEnd = this._connectEnd.Perceptron;

                if (perceptronEnd.addInputChannel(perceptronStart) == true)
                    this._connectEnd.DrawClientConnectionsAutomatically();

                this._connectStart = null;
                this._connectEnd = null;
            }
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ctrlPerceptronDrawingBoard1.openFile(sender, e);
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ctrlPerceptronDrawingBoard1.saveFile(sender, e);
        }

        private void clearToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ctrlPerceptronDrawingBoard1.clearDrawingBoard(sender, e);
        }

        private void quitToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void deleLineToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ctrlTeachingConsole1.DeleteLine();
        }

        private void loadToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ctrlTeachingConsole1.LoadScript(sender, e);
        }

        private void saveToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            ctrlTeachingConsole1.SaveScript(sender, e);
        }

        private void startToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ctrlTeachingConsole1.StartTraining();
        }

        private void stopToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ctrlTeachingConsole1.StopTraining();
        }

        private void insertLineToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ctrlTeachingConsole1.InsertLine();
        }

        private void clearAllLinesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ctrlTeachingConsole1.ClearGrid(sender, e);
        }

        private void updateGridToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ctrlTeachingConsole1.UpdateGrid();
        }

        private void exportNetworkToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ctrlPerceptronDrawingBoard1.exportFile(sender, e);
        }

        private void importNetworkToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ctrlPerceptronDrawingBoard1.importFile(sender, e);
        }

        private void StepDone()
        {
            toolStripStatusLabel4.Text = "Events:" + Convert.ToString(this._backbone.EventsThrownByActrons);
            this._backbone.resetEventCounter();
        }

        private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            this._backbone.ShutDown();
            this.ctrlTCPLogger1.Disconnect();
        }
    }
}
